﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Benefit.Models.Sys;
namespace Benefit.Service.Day
{
    public class ServiceAccountDayTradeProduct : Benefit.Interface.Day.IAccountDayTradeProduct
    {
        Benefit.DB.DBManager db = null;
        public ServiceAccountDayTradeProduct(Benefit.DB.DBManager db)
        {
            this.db = db;
        }



        /// <summary>
        /// 删除当日所有记录
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId)
        {
            var query = from t in db.AccountDayTradeProduct where t.TradeHistoryId == tradeHistoryId select t;
            foreach (Models.Day.AccountDayTradeProduct c in query)
            {
                db.AccountDayTradeProduct.Remove(c);
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 保存账号日变化情况
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitDayAccountProductChange(int tradeHistoryId, string pdate)
        {
            Interface.ServerData.IT_Account ita = new ServerData.ServiceT_Account(db);
            Interface.Sys.IAccount iacount = new Sys.ServiceAccount(db);
            Interface.ServerData.IT_TradeDetail_History tth = new ServerData.ServiceT_TradeDetail_History(db);
            Interface.Sys.IProduct ip = new Sys.ServiceProduct(db);
            Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
            Interface.ServerData.IT_PositionDetail_History itp = new ServerData.ServiceT_PositionDetail_History(db);
            List<Models.ServerData.T_Account> serverAccounts = ita.GetList();
            foreach (Models.ServerData.T_Account account in serverAccounts)
            {
     
                //   Data.ServerData.T_TradeDetail_History tth = new Data.ServerData.T_TradeDetail_History();
                List<string> products = tth.GetAccountDayProduct(pdate, account.Id);
                foreach (string product in products)
                {
                    #region i为0是多，i为1是空
                    for (int i = 0; i < 2; i++)
                    {
                        Models.Day.AccountDayTradeProduct change = new Models.Day.AccountDayTradeProduct();
                 
                        // Sys.Account _account = new Sys.Account();
                        change.AccountId = iacount.GetAccountId(account.LoginAccount);
                        change.TradeHistoryId = tradeHistoryId;
                        change.Direction = i;
                        change.BillCount = tth.GetDayProductBillCount(pdate, account.Id, product, change.Direction.ToString());
                        change.Free = tth.GetDayProductCommission(pdate, account.Id, product, change.Direction.ToString());
            
                        change.ProductId = ip.GetProductId(product);
                        change.LoseCount = tth.GetDayProductLoseCount(pdate, account.Id, product, change.Direction.ToString());
                        change.Profit = tth.GetDayProductCloseProfit(pdate, account.Id, product, change.Direction.ToString());
                        change.DayCount = change.Profit - change.Free;
                        change.SumLost = tth.GetDayProductAllLose(pdate, account.Id, product, change.Direction.ToString());
                        change.SumWin = tth.GetDayProductAllWin(pdate, account.Id, product, change.Direction.ToString());
     
           
                        change.UsedMargin = itp.GetAccountProduct(account.Id, ith.GetPdateFromSettlementId(tradeHistoryId), product);
                        change.WinCount = tth.GetDayProductWinCount(pdate, account.Id, product, change.Direction.ToString());
                        db.AccountDayTradeProduct.Add(change);
                    }
                    #endregion
                }
            }
            db.SaveChanges();
        }



        public List<Models.Day.AccountDayTradeProduct> GetAccountDayTradeProduct(int accountid, int tradeHistoryId)
        {
            List<Models.Day.AccountDayTradeProduct> instruments = new List<Models.Day.AccountDayTradeProduct>();
            var query = db.AccountDayTradeProduct.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId);
            if (query.Count() > 0)
            {
                instruments = query.ToList();
            }
            return instruments;
        }

        public List<Models.Day.AccountDayTradeProduct> GetAccountDayTradeProduct(int accountid, int tradeHistoryId, bool win)
        {
            List<Models.Day.AccountDayTradeProduct> instruments = new List<Models.Day.AccountDayTradeProduct>();
            List<Models.Day.AccountDayTradeProduct> query = null;
            if (win)
            {
                query = db.AccountDayTradeProduct.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId).Where(a => a.Profit > 0).ToList();

            }
            else
            {
                query = db.AccountDayTradeProduct.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId).Where(a => a.Profit <= 0).ToList();
            }
            if (query.Count() > 0)
            {
                instruments = query.ToList();
            }
            return instruments;
        }
    }
}